package Chuong06;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

public class Bai07 extends JFrame implements Runnable, MouseMotionListener{

	int x = 0, y = 0;
	
	public Bai07(){
		super("Bài 07 - Chương 06");
		setVisible(true);
		setSize(600, 600);
		addMouseMotionListener(this);
		new Thread(Bai07.this).start();
	}
	
	public static void main(String[] args) {
		Bai07 frm = new Bai07();		
	}

	@Override
	public void run() {
		Graphics g = this.getGraphics();
		int dy=1;
		
		while(true){
			
			g.setColor(Color.RED);
			g.fillOval(x, y, 20, 20);
			
			try{ 
				Thread.sleep(5);
			}
			catch(Exception e){}
			
			g.setColor(this.getBackground());
			g.fillOval(x, y, 20, 20);
		
			y += dy;
			if(y < 0 || y > this.getHeight()-20) dy=-dy;
		}
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {		
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		x = e.getX();
		
	}

}
